package val.to.common;

import val.to.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class SingleItemViewAdapter extends BaseAdapter{
	
	private LayoutInflater m_inflater;
	private int mMessageId;
	private int mImageId = -1;
	private boolean mShowProgress;
	private String mDetails;
	
	public SingleItemViewAdapter(Context ctx, int messageId, boolean showProgress){
		m_inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMessageId = messageId;
		mShowProgress = showProgress;
	}
	
	public SingleItemViewAdapter(Context ctx, int messageId, boolean showProgress, int imageId){
		this(ctx, messageId, showProgress);
		mImageId = imageId;
	}

	public SingleItemViewAdapter(Context ctx, int messageId, boolean showProgress, int imageId, String details){
		this(ctx, messageId, showProgress, imageId);
		mDetails = details;
	}
	
	@Override
	public int getCount() {
		return 1;
	}

	@Override
	public Object getItem(int arg0) {
		return new Object();
	}

	@Override
	public long getItemId(int position) {
		return 1;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
        	return convertView;
        }

        convertView = m_inflater.inflate(R.layout.message_item, null);
        TextView msg = (TextView) convertView.findViewById(R.id.user_message);
        TextView details = (TextView) convertView.findViewById(R.id.details);
        ProgressBar prog = (ProgressBar) convertView.findViewById(R.id.progress);
               
        prog.setVisibility(mShowProgress ? View.VISIBLE : View.GONE);
        msg.setText(mMessageId);
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        image.setVisibility(mImageId != -1 ? View.VISIBLE : View.GONE);
        if (mImageId != -1){
            image.setImageResource(mImageId);       	
        }

        final Context ctx = convertView.getContext();
        if (mDetails == null){
        	details.setVisibility(View.GONE);
        }
        else{
        	details.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					 AlertDialog.Builder builder = new AlertDialog.Builder(ctx);  
					 builder.setMessage(mDetails)
					.setTitle(R.string.error_details_title)
					.setIcon(android.R.drawable.ic_dialog_info)
					.setPositiveButton("Close", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int id) {
					    	dialog.dismiss();
					    }
					});
					builder.show();					
				}
			});
        }
        
        return convertView;
    }
}
